package com.roamer.rest;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

import java.io.Serializable;

import static com.roamer.rest.ResultCode.ErrorCode;
import static com.roamer.rest.ResultCode.SuccessCode;

/**
 * 统一数据格式
 *
 * @author roamer
 * @version V1.0
 * @date 2019/1/20 19:21
 */
@Getter
@ToString
@EqualsAndHashCode
public class RestResult<T> implements Serializable {

    private static final long serialVersionUID = -381393050079498801L;

    /** 成功 */
    private boolean success;

    /** 响应码 */
    private int code;

    /** 消息 */
    private String message;

    /** 数据 */
    private T data;

    /** 服务器时间 */
    private Long timestamp;

    public RestResult(boolean success, int code, String message, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }

    public RestResult(ResultCode resultCode, T data) {
        this(resultCode);
        this.data = data;
    }

    public RestResult(ResultCode resultCode) {
        this.success = resultCode.success();
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.timestamp = System.currentTimeMillis();
    }

    /**
     * 成功
     *
     * @return {@code ResultBean<?>}
     */
    public static RestResult<?> success() {
        return new RestResult(ISuccessCode.OK);
    }

    /**
     * 成功
     *
     * @param data 需要返回的数据
     * @param <E>  数据类型
     *
     * @return {@code ResultBean<E>}
     */
    public static <E> RestResult<E> success(E data) {
        return new RestResult<>(ISuccessCode.OK, data);
    }


    /**
     * 成功
     *
     * @param successCode 成功状态码
     *
     * @return {@code ResultBean<?>}
     */
    public static RestResult<?> success(SuccessCode successCode) {
        return new RestResult<>(ISuccessCode.OK);
    }

    /**
     * 成功
     *
     * @param successCode 成功状态码
     * @param data        需要返回的数据
     * @param <E>         数据类型
     *
     * @return {@code ResultBean<E>}
     */
    public static <E> RestResult<E> success(SuccessCode successCode, E data) {
        return new RestResult<>(successCode, data);
    }

    /**
     * 失败
     *
     * @param message 消息提示
     *
     * @return {@code ResultBean<?>}
     */
    public static RestResult<?> failure(String message) {
        RestResult<?> result = new RestResult<>(IErrorCode.ERROR);
        result.message = message;
        return result;
    }

    /**
     * 失败
     *
     * @param message 消息提示
     * @param data    需要返回的数据
     * @param <E>     数据类型
     *
     * @return {@code ResultBean<E>}
     */
    public static <E> RestResult<E> failure(String message, E data) {
        RestResult<E> result = new RestResult<>(IErrorCode.ERROR, data);
        result.message = message;
        return result;
    }

    /**
     * 失败
     *
     * @param errorCode 错误状态码
     *
     * @return {@code ResultBean<?>}
     */
    public static RestResult<?> failure(ErrorCode errorCode) {
        return new RestResult<>(errorCode);
    }

    /**
     * 失败
     *
     * @param errorCode 错误状态码
     * @param data      需要返回的数据
     * @param <E>       数据类型
     *
     * @return {@code ResultBean<E>}
     */
    public static <E> RestResult<E> failure(ErrorCode errorCode, E data) {
        return new RestResult<>(errorCode, data);
    }

}
